import requests
import tkinter as tk
from tkinter import filedialog
import json


def save(fileurl):
    url = r"http://192.168.0.190:8851/default/faqs/save"
    headers = {
        "accept": "application/json, text/plain, */*",
        "accept-encoding": "gzip, deflate",
        "accept-language": "en",
        "cache-control": "no-cache",
        "connection": "keep-alive",
        "content-type": "application/json",
        "cookie": "header_left_menu=[]; access_token=424187dc5449e6e7b30681941dbf92e4; USER_Id=1199596756811550720; SECKEY_ABVK=FO8ZK6fCUPPagddQx3ejofwSDMlq3kb3Zuu7AecjyJ0%3D; BMAP_SECKEY=KrPzLIkDQtdzxcYDhy9qXdjo4ToqB8kCae8e01klEa7E-pjdcHUVKbNyhCNoNCKxOzILZtdu7yZCTT53SWOI1Z0dcE7EPSauYG7nMJPEReZu2hPMML6HxKk1alKDG03_8jCbvW9Yt_Vvp0Qoi9uWU1_8S7-xFYI_FKaUKkr49BPYzqdtEDz7oHTABK6aO9XX",
        "host": "192.168.0.190:8851",
        "origin": "http://192.168.0.190:8851",
        "pragma": "no-cache",
        "referer": "http://192.168.0.190:8851/",
        "source": "Web",
        "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/139.0.0.0 Safari/537.36",
        "x-access-token": "424187dc5449e6e7b30681941dbf92e4",
    }

    params = {
        "id": "1958482379676487680",
        "serviceId": "General",
        "name": "1视频链接",
        "content": "<p><span>2222222221</span></p>",
        "status": 1,
        "createTimeStr": "2025/08/21",
        "link": fileurl,
        "linkType": 2,
        "showSelectPanel": False,
        "showDeleteIcon": False,
        "organizations": [
            "resident"
        ]
    }

    # ⚠️ 用 json=params，而不是 data=params
    response = requests.post(url, headers=headers, json=params)
    print(response.text)


def upload_file():
    # 弹出选择文件对话框
    root = tk.Tk()
    root.withdraw()  # 隐藏主窗口
    file_path = filedialog.askopenfilename(title="选择要上传的文件")
    if not file_path:
        print("❌ 未选择文件")
        return None

    url = r"https://cloud.connectiot.ai/connect-iot/file/static"

    headers = {
        "accept": "application/json, text/plain, */*",
        "accept-encoding": "gzip, deflate, br, zstd",
        "accept-language": "en",
        "cache-control": "no-cache",
        "connection": "keep-alive",
        "content-length": "18542824",
        "content-type": "multipart/form-data; boundary=----WebKitFormBoundarymStlex5YUAQOvnJH",
        "cookie": "header_service=; header_building_label=; header_left_menu=[]; USER_Id=54f1e4c713f00aa5e3ce319e79525ac2; access_token=acde0729690b04f5eac98c6cc968166f",
        "host": "cloud.connectiot.ai",
        "origin": "https://cloud.connectiot.ai",
        "pragma": "no-cache",
        "referer": "https://cloud.connectiot.ai/",
        "sec-ch-ua": "\"Not;A=Brand\";v=\"99\", \"Google Chrome\";v=\"139\", \"Chromium\";v=\"139\"",
        "sec-ch-ua-mobile": "?0",
        "sec-ch-ua-platform": "\"Windows\"",
        "sec-fetch-dest": "empty",
        "sec-fetch-mode": "cors",
        "sec-fetch-site": "same-origin",
        "source": "Web",
        "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/139.0.0.0 Safari/537.36",
        "x-access-token": "acde0729690b04f5eac98c6cc968166f"
    }

    # 根据文件类型推测 MIME（可以更智能，这里简单处理）
    mime_type = "application/octet-stream"
    if file_path.endswith(".png"):
        mime_type = "image/png"
    elif file_path.endswith(".jpg") or file_path.endswith(".jpeg"):
        mime_type = "image/jpeg"
    elif file_path.endswith(".mp4"):
        mime_type = "video/mp4"

    files = {
        "file": (file_path.split("/")[-1], open(file_path, "rb"), mime_type)
    }

    resp = requests.post(url, headers=headers, files=files)

    file_url = json.loads(resp.text)

    print("📤 上传文件:", file_path)
    print("状态码:", resp.status_code)
    print("响应:", resp.text)
    print("文件链接:", file_url)
    return file_url


file_url1 = upload_file()
# save(file_url1)
